﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace App.Store
{
    public partial class frmStoreManagement : Form
    {
        Entities db = new Entities();

        T_Store m_Store = null;
        public int X { get; set; }

        public string m_Location
        {
            set
            {
                var list = db.T_Store.Where(a => a.location == value);
                foreach (T_Store store in list)
                {
                    m_Store = store;
                    break;
                }
            }
        }
        public frmStoreManagement()
        {
            InitializeComponent();
            gvDataSource();
            //GetLocation();
        }

        private void frmStoreManagement_Load(object sender, EventArgs e)
        {
            //Service.Ktype(cbkType);
        }

        public void gvDataSource()
        {
            var storelist = db.T_Store.OrderBy(a=>a.kType).ToList();
            bs1.DataSource = storelist;
            bnav.BindingSource = bs1;

            txtRCount.Text = db.T_Store.Sum(a=>a.capability).ToString();
            txtsCount.Text = db.T_Store.Sum(a => a.sCount).ToString();
            txtRealCount.Text = db.T_Store.Sum(a => a.realStore).ToString();

        }

        public void GetLocation()
        {
            if (gvlist.RowCount > 0)
            {
                int index = gvlist.CurrentRow.Index;    //取得选中行的索引  
                this.m_Location = gvlist.Rows[index].Cells["collocation"].Value.ToString();
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.txtBeginlocation.Text == null || this.txtBeginlocation.Text == "" || this.txtEndlocation.Text == null || this.txtEndlocation.Text == "")
            {
                MessageBox.Show("请输入区号");
                return;
            }

            if (this.cbkType.Text == "" || this.cbkType.Text == null)
            {
                MessageBox.Show("请选择仓位性质");
                return;
            }

            if (this.txtcapability.Text == null || Convert.ToInt32(this.txtcapability.Text) <= 0)
            {
                MessageBox.Show("请输入容量");
                return;
            }

            int Begin;
            int End;
            String strHead = "";
            if ((this.txtBeginlocation.Text.Length == 2 && this.txtEndlocation.Text.Length == 2) ||
                (this.txtBeginlocation.Text.Length == 1 && this.txtEndlocation.Text.Length == 1))
            {
                if (this.txtBeginlocation.Text.Length == 2)
                {
                    strHead = this.txtBeginlocation.Text.Substring(0, 1);
                    Begin = Convert.ToInt32(Convert.ToChar(this.txtBeginlocation.Text.Substring(1, 1)));
                    End = Convert.ToInt32(Convert.ToChar(this.txtEndlocation.Text.Substring(1, 1)));
                }
                else
                {
                    Begin = Convert.ToInt32(Convert.ToChar(this.txtBeginlocation.Text));
                    End = Convert.ToInt32(Convert.ToChar(this.txtEndlocation.Text));
                }

                if (Begin > End)
                {
                    MessageBox.Show("请按照由小到大的字母或数字顺序排列区号");
                    return;
                }
            }
            else
            {
                MessageBox.Show("非法区号！请重新输入");
                return;
            }

            int col1 = Convert.ToInt32(this.txtBeginLine.Text);
            int col2 = Convert.ToInt32(this.txtEndLine.Text);
            if (col1 > col2)
            {
                MessageBox.Show("请按照由小到大顺序排列列号");
                return;
            }
            int row1 = Convert.ToInt32(this.txtBeginRows.Text);
            int row2 = Convert.ToInt32(this.txtEndRows.Text);
            if (row1 > row2)
            {
                MessageBox.Show("请按照由小到大顺序排列行号");
                return;
            }
            String location = null;
            for (int i = Begin; i <= End; i++)//对区号循环
            {
                for (int n = row1; n <= row2; n++)//对行循环
                {
                    for (int m = col1; m <= col2; m++)//对列循环
                    {
                        location = location + strHead + Convert.ToChar(i).ToString() + n.ToString() + "-" + m.ToString() + ",";
                    }
                }
            }
            try
            {
                m_Store = new T_Store();
                m_Store.location = location;
                if(!string.IsNullOrEmpty(txtcapability.Text)) m_Store.capability = Convert.ToInt32(txtcapability.Text);
                if (!string.IsNullOrEmpty(txtrealStore.Text)) m_Store.sCount = Convert.ToInt32(txtrealStore.Text);
                m_Store.kType = cbkType.Text;
                m_Store.realStore = 0;
                db.T_Store.Add(m_Store);
                int i = db.SaveChanges();
                Msg.ShowInfo(string.Format("已保存{0}条仓库信息。", i));
                gvDataSource();

            }
            catch
            {
                MessageBox.Show("保存失败");
            }
        }

        private void btncalloff_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnClean_Click(object sender, EventArgs e)
        {
            GetLocation();
            if (Msg.ShowWarning("您确定要清仓吗？") == System.Windows.Forms.DialogResult.Yes)
            {
                if (m_Store == null) return;
                m_Store.sCount = 0;
                m_Store.realStore = 0;
                db.SaveChanges();

                Msg.ShowInfo("清仓成功！");
                gvDataSource();
            }
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
